/**
 * Copyright 2009 the original author or authors.
 *
 * Licensed under the GPL License, Version 3.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.gnu.org/licenses/lgpl-3.0.txt
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.guardianapis.api.http;

import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class SimpleHttpClient implements GuardianHttpClient {

	/*******************************************************************************
	 * Constants
	 *******************************************************************************/

	private static Log log = LogFactory.getLog(SimpleHttpClient.class);
	private static final int HTTP_READ_BUFFER = 1024;

	/*******************************************************************************
	 * Properties
	 *******************************************************************************/

	public GuardianHttpResponse requestUrl(String pUrl) {
		String currentUrl = pUrl;
		GuardianHttpResponse response;
		URL url;
		InputStream httpInputStream = null;
		ByteArrayOutputStream httpResponseBuffer = null;
		
		log.debug("Making browser request to: " + currentUrl);
		try {
			url = new URL(currentUrl);
			httpInputStream = new BufferedInputStream(url.openStream());
			httpResponseBuffer = new ByteArrayOutputStream();
			byte[] readBuffer = new byte[HTTP_READ_BUFFER];
			int numBytesRead = 0;
			
			while ((numBytesRead = httpInputStream.read(readBuffer)) != -1) {
				httpResponseBuffer.write(readBuffer, 0, numBytesRead);
			}
			byte[] httpResponseBytes = httpResponseBuffer.toByteArray();
			
			if (log.isTraceEnabled()) {
				log.trace("Raw response: " + new String(httpResponseBytes));
			}
			
			InputStream responseStream = new ByteArrayInputStream(httpResponseBytes);
			response = new GuardianHttpResponse(responseStream);
			
		} catch (Exception e) {
			if (httpInputStream != null) { 
				try {
					httpInputStream.close();
				} catch (IOException e1) {
					log.error("Error closing HTTP input stream",e);
				} 
			}
			if (httpResponseBuffer != null) {
				try {
					httpResponseBuffer.close();
				} catch (IOException e1) {
					log.error("Error closing HTTP response stream",e);
				}
			}
			log.error("Error while making HTTP request to: " + currentUrl,e);
			response = new GuardianHttpResponse("Failed HTTP request: " + e.getMessage(),e);
		}
		return response;
	}

}
